<script setup lang="ts">
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import service from '@/utils/request'
import { useUserStore } from "@/stores/counter.js"

const router = useRouter()
const route = useRoute()

const form = ref({
  username: 'bianque',
  password: 'bianque123'
})

const loading = ref(false)
const redirect = ref((route.query.redirect as string) || '/') // 现在不会报错

const login = async () => {
  try {
    loading.value = true;
    
    const userStore = useUserStore();
    const user = await userStore.login(form.value);
    

    
    ElNotification.success({
      title: '登录成功',
      message: `欢迎回来，${user.username}`,
    });
    
    router.push('/layout');
  } catch (error) {
    ElNotification.error({
      title: '登录失败'
    });
  } finally {
    loading.value = false;
  }
};

</script>

<template>
  <div class="login-container">
    <el-form 
      :model="form" 
      label-width="80px"
      @submit.prevent="login"
    >
      <h2 class="title">系统登录</h2>
      
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="form.username"
          placeholder="请输入用户名"
          clearable
        />
      </el-form-item>
      
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
          show-password
          clearable
          @keyup.enter="login"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button
          type="primary"
          :loading="loading"
          native-type="submit"
          style="width: 100%"
        >
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
}

.el-form {
  width: 400px;
  padding: 30px;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
</style>